/**
 * 
 */
package com.ekingstar.framework.action;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonBeanProcessor;
import net.sf.json.processors.JsonBeanProcessorMatcher;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.Element;
import com.ekingstar.framework.domain.Function;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.IFunctionService;
import com.ekingstar.framework.service.IUserService;

/**
 * 
 *
 */

@SuppressWarnings("serial")
@Action(name="myFavarites",namespace="/user")
@Result(name="input",value="/user/myFavarites-input.jsp")
public class FavaritesAction extends ActionSupport{
	
	
	private IUserService userService;
	private IFunctionService functionService;
	@Element(Function.class)
	private List funs;
	public List getFuns() {
		return funs;
	}

	public void setFuns(List funs) {
		this.funs = funs;
	}

	private String node;
	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	//
	public String execute(){
		
		return "input";
	}

	public String save(){
		userService.addOrUpdateMyFavarites(UserUtil.getCurrentUserId(), funs);
		return "input";
		
	}
	public String getMyFunctions(){
		try{
		List functions = functionService.getFunctionsByUserId(UserUtil.getCurrentUserId());
		final List userFav = userService.getMyFavarites(UserUtil.getCurrentUserId());
		
		HttpServletResponse res =  ServletActionContext.getResponse();
		res.setContentType("text/paint; charset=UTF-8");
		//
		final JsonConfig jc = new JsonConfig();
		jc.registerJsonBeanProcessor(Function.class, new JsonBeanProcessor(){
			public JSONObject processBean(Object bean, JsonConfig jsonConfig) {
				// TODO Auto-generated method stub
				Function value = (Function)bean;
				JSONObject rv = new JSONObject();
				rv.element("text", value.getName());
				rv.element("id", value.getId());
				rv.element("children", value.getChilden(),jc);
				if (value != null && Boolean.TRUE.equals((value.getLeaf()))){
					if (userFav != null&& !userFav.contains(value)){
						rv.element("checked", false);
					}
					else {
						rv.element("checked", true);
					}
				}
				return rv;
			}
		});
		jc.setJsonBeanProcessorMatcher(new JsonBeanProcessorMatcher(){
			@Override
			public Object getMatch(Class target, Set set) {
				// TODO Auto-generated method stub
				if (target != null && set != null){
					if (set.contains(target)){
						return target;
					}
					return getMatchFromSuper(target,set);
				}
				return null;
			}
			private Object getMatchFromSuper(Class target,Set set){
				Class superCls = target.getSuperclass();
				if (superCls != null && set.contains(superCls)){
					return superCls;
				}
				else if (superCls != null ){
					return getMatchFromSuper(superCls,set);
				}
				Class[] interfaceCls = target.getInterfaces();
				if (interfaceCls != null  && interfaceCls.length >0){
					for (int i = 0 ;i < interfaceCls.length;i++){
						if (interfaceCls[i] != null && set.contains(interfaceCls[i])){
							return interfaceCls[i];
						}
						else if (interfaceCls[i] != null ){
							return getMatchFromSuper(interfaceCls[i],set);
						}
					}
				}
				return null;
			}
			
		});
		JSONArray funcs = JSONArray.fromObject(functions,jc);
		res.getWriter().print(funcs.toString());
		}catch(Throwable e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void setFunctionService(IFunctionService functionService) {
		this.functionService = functionService;
	}

}
